#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2019/2/18 19:29
# @Author  : yangdy
# @Email   : yangdy@egu360.com
# @File    : 删除排序数组中的重复项 II.py
# @Software: PyCharm


class Solution:
    def removeDuplicates(self, nums: 'List[int]') -> 'int':
        ll = len(nums)
        if ll <= 0:
            return 0
        i, p, j, ll = 0, 1, 1, ll
        while j < ll:
            if nums[j] == nums[i]:
                if p < 2:
                    nums[i+1] = nums[i]
                    i += 1
                    p += 1
                    j += 1
                else:
                    j += 1
            else:
                nums[i+1] = nums[j]
                i, p, j = i+1, 1, j+1
        return i+1


solve = Solution()

nums = [1, 1, 1, 2, 2, 3]
print(nums)
print(solve.removeDuplicates(nums))
print(nums)

nums = [0, 0, 1, 1, 1, 1, 2, 3, 3]
print(nums)
print(solve.removeDuplicates(nums))
print(nums)

